/* eslint-disable @typescript-eslint/no-explicit-any */
import Vue from 'vue';
import Vuex from 'vuex';
import request from '@/request/index';
import VueXAlong from 'vuex-along';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    categories: [],
    childCategories: [],
    currentUser: null,
    token: '',
    tokenHead: '',
    shoppingCartItems: [],
    currSettlingProducts: [],
    currPayableTotalAmount: 0,
    currReceiveAddressForm: {
      name: '',
      tel: '',
      postCode: '',
      province: '',
      city: '',
      region: '',
      detailAddress: ''
    },
    currPickDist: null,
    defaultReceiveAddress: {
      name: '',
      tel: '',
      postCode: '',
      province: '',
      city: '',
      region: '',
      detailAddress: ''
    }
  },
  mutations: {
    setCategories(state, categories) {
      state.categories = categories;
    },
    setChildCategories(state, index) {
      state.childCategories = (state.categories[index] as any).children;
    },
    setCurrentUser(state, user) {
      state.currentUser = user;
    },
    setToken(state, { token, tokenHead }) {
      state.token = token;
      state.tokenHead = tokenHead;
    },
    clearCurrentUser(state) {
      state.currentUser = null;
      state.token = '';
      state.tokenHead = '';
    },
    setShoppingCartItems(state, cartItems) {
      state.shoppingCartItems = cartItems;
    },
    setCurrSettlingProducts(state, products) {
      state.currSettlingProducts = products;
      state.currPayableTotalAmount = state.currSettlingProducts
        .map((each: any) => each.price)
        .reduce((prev, curr) => prev + curr);
    },
    saveCurrReceiveAddressForm(state, form) {
      state.currReceiveAddressForm = form;
    },
    clearCurrReceiveAddressForm(state) {
      state.currReceiveAddressForm = {
        name: '',
        tel: '',
        postCode: '',
        province: '',
        city: '',
        region: '',
        detailAddress: ''
      };
      state.currPickDist = null;
    },
    saveCurrDistInfo(state, dist) {
      state.currPickDist = dist;
      state.currReceiveAddressForm = {
        ...state.currReceiveAddressForm,
        ...dist
      };
    },
    setDefaultReceiveAddress(state, addr) {
      state.defaultReceiveAddress = addr;
    }
  },
  actions: {
    queryCategories({ commit }, callback) {
      request.get('/products/categories').then((res: any) => {
        commit('setCategories', res);
        commit('setChildCategories', 0);
        callback();
      });
    },
    refreshChildCategories({ commit }, index) {
      commit('setChildCategories', index);
    },
    login({ commit }: any, { userParam, successCallback, errorCallback }) {
      request.post('/auth/login', userParam).then((res: any) => {
        if (res.success) {
          commit('setToken', { token: res.token, tokenHead: res.tokenHead });
          successCallback();
        } else {
          errorCallback();
        }
      });
    },
    logout({ commit }, callback) {
      request.get('/auth/logout').then(() => {
        commit('clearCurrentUser');
        callback();
      });
    },
    refreshShoppingCartItems({ commit }, callback) {
      request
        .get('/cartItems', {
          params: {
            appUserId: (this.state.currentUser as any).id
          } as any
        })
        .then((resp: any) => {
          commit('setShoppingCartItems', resp);
          callback && callback();
        });
    },
    getDefaultReceiveAddress({ commit }) {
      request
        .get('/userReceiveAddress', {
          params: {
            appUserId: (this.state.currentUser as any).id,
            defaultStatus: 1
          } as any
        })
        .then((resp: any) => {
          if (resp && resp.length === 1) {
            commit('setDefaultReceiveAddress', resp[0]);
          } else {
            commit('setDefaultReceiveAddress', {
              name: '',
              tel: '',
              postCode: '',
              province: '',
              city: '',
              region: '',
              detailAddress: ''
            });
          }
        });
    }
  },
  modules: {},
  plugins: [
    VueXAlong({
      name: 'along'
      // local: false,
      // session: { list: [], isFilter: true }
    })
  ]
});
